package com.cdfahe.gms.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cdfahe.common.Constants;
import com.cdfahe.common.util.page.ResultFilter;
import com.cdfahe.gms.mybatis.entity.Drug;
import com.cdfahe.gms.mybatis.entity.DrugExample;
import com.cdfahe.gms.mybatis.mapper.DrugMapper;
import com.cdfahe.gms.service.IDrugService;
@Service
public class DrugServiceImpl implements IDrugService{
	@Resource
	private DrugMapper drugMapper;
	
	@Override
	public ResultFilter<Drug> listDrugs(ResultFilter<Drug> rf, int type, int pagesize) {
		rf.setPageSize(pagesize);
		rf.setTotalCount(getTotalDrugByType(type));  
	    rf.setItems(selectDrugByPageAndType(type, (rf.getCurrentPage() - 1 )* rf.getPageSize(), rf.getPageSize()));
		return rf;  
	}

	@Override
	public int getTotalDrugByType(int type) {
		DrugExample example = new DrugExample();
		if (type != Constants.SELECT_ALL_DRUG) {
			example.createCriteria().andDrugtypeEqualTo(type);
		}
		return drugMapper.countByExample(example);
	}

	@Override
	public Drug getDrugById(int idnum) {
		return drugMapper.selectByPrimaryKey(idnum);
	}

	@Override
	public void addDrug(Drug drug) {
		drugMapper.insert(drug);
	}

	@Override
	public void delDrugById(int idnum) {
		drugMapper.deleteByPrimaryKey(idnum);
	}

	@Override
	public void updateDrugById(Drug drug) {
		drugMapper.updateByPrimaryKeySelective(drug);
	}

	@Override
	public void updateDrugByExample(DrugExample example) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Drug> selectDrugByExample(DrugExample example) {
		return drugMapper.selectByExampleWithBLOBs(example);
	}

	@Override
	public List<Drug> selectDrugByPageAndType(int type, int start, int end) {
		if (type != Constants.SELECT_ALL_DRUG) {
			return drugMapper.selectDrugByPageAndType(type, start, end);
		} else {
			return drugMapper.selectDrugByPage(start, end);
		}
	}
}
